package com.ethan.controller;

import com.ethan.pojo.Result;
import com.ethan.utils.AliOssUtil;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@RestController
public class FileUploaderController {
    @PostMapping("/uploader")
    public Result<String> upload(MultipartFile file) throws Exception {
        //把文件存储到本地磁盘
        String originalFilename = file.getOriginalFilename();
        //保证文件名字唯一，防止文件被覆盖，解决方案uuid
        String fileName = UUID.randomUUID() +originalFilename.substring(originalFilename.lastIndexOf("."));
        //file.transferTo(new File("/Users/ethan/Desktop/file/"+fileName));
        String URL = AliOssUtil.uploadFile(fileName,file.getInputStream());
        return Result.success(URL);
    }
}
